package com.lagou.work5.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client1 {
    public static void main(String[] args) {

        try {
            Socket socket = new Socket("127.0.0.1",8888);
            System.out.print("进入聊天室前，请输入您的名字:");
            Scanner scanner = new Scanner(System.in);
            String userName = scanner.next();

            // 初始化本地资源目录

            File userLocalStorage = new File("/Users/dingxiaowei/Downloads/client/"+userName);
            if(!userLocalStorage.exists()){
                userLocalStorage.mkdirs();
            }
            System.out.println("用户本地资源目录已创建成功");

            // 获取输出流，向server发送登录用户名
            PrintStream printStream = new PrintStream(socket.getOutputStream());
            printStream.println(userName);


            System.out.println("-----------------------您已进入聊天室，请畅所语言-----------------------");

            // 创建一个线程，不断接收聊天室的信息
            Thread thread = new Thread(new ClientReceiver(socket));
            thread.start();

            //聊天室聊天
            while (true){
               String userMessage =  scanner.next();
               if("sendFile".equals(userMessage)){
                   System.out.print("请输入要发送的文件名:");
                   String fileName = scanner.next();
                   File file1 = new File("/Users/dingxiaowei/Downloads/client/"+userName+"/"+fileName);
                   // 判断是否为目录
                   if(file1.isDirectory()){
                       System.out.println("不支持发送目录文件");
                       continue;
                   }
                   // 判断文件是否存在
                   if(file1.exists()){
                       // 1、发送 send 关键字，告诉服务器，我要上传文件
                       printStream.println(userMessage);
                       // 2、发送文件名
                       printStream.println(fileName);

                       // 3、发送文件
                       BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file1));

                       // socket
                       BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
                       int res = 0;
                       while ((res=bufferedInputStream.read())!=-1){
                           // 将数据发送到服务器
                           bufferedOutputStream.write(res);
                           bufferedOutputStream.flush();

                       }
                       System.out.println("文件上传成功！");

                   }else {
                       System.out.println("文件不存在");
                   }
               }else if ("downloadFile".equals(userMessage)){
                   System.out.print("请输入文件名字：");
                   String fileName = scanner.next();
                   // 1、将文件名发送至服务器，
                   printStream.println(userMessage);

                   printStream.println(fileName);

                   // 2、读取文件
                   int res =0;
                   BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
                   //首先判断文件是否存在
                   File file1 = new File("/Users/dingxiaowei/Downloads/client/"+userName+"/"+fileName);
                   if(file1.exists()){
                       System.out.println("文件已存在");
                   }else {
                       BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1));
                       while((res = bufferedInputStream.read())!=-1){
                           bufferedOutputStream.write(res);
                           bufferedOutputStream.flush();
                       }
                       System.out.println("文件已经下载至本地");
                   }

               }else if("bye".equals(userMessage)){
                   System.out.println("退出聊天室");
                   break;
               }else {
                   // 发送消息给服务器
                   printStream.println(userMessage);
               }

            }



        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
